package com.agile.leetcode.tomcat.abstractservlet;

import com.agile.leetcode.tomcat.netty.NettyRequest;
import com.agile.leetcode.tomcat.netty.NettyResponse;
import com.agile.leetcode.tomcat.nio.MyNioRequest;
import com.agile.leetcode.tomcat.nio.MyNioResponse;
import com.agile.leetcode.tomcat.normalio.MyRequest;
import com.agile.leetcode.tomcat.normalio.MyResponse;

import java.util.Objects;

/**
 * 自己的servlet抽象类
 *
 * @Author KD
 * @Date 2021/2/6 14:06
 */
public abstract class MyHttpServlet {
    /**
     * netty get请求
     *
     * @param request
     * @param response
     */
    public abstract void doGet(NettyRequest request, NettyResponse response);

    /**
     * netty 的post请求
     *
     * @param request
     * @param response
     */
    public abstract void doPost(NettyRequest request, NettyResponse response);
    /**
     * io get请求
     *
     * @param request
     * @param response
     */
    public abstract void doGet(MyRequest request, MyResponse response);

    /**
     * nio 的get请求
     *
     * @param request
     * @param response
     */
    public abstract void doGet(MyNioRequest request, MyNioResponse response);

    /**
     * post请求
     *
     * @param request
     * @param response
     */
    public abstract void doPost(MyRequest request, MyResponse response);

    /**
     * nio 的post请求
     *
     * @param request
     * @param response
     */
    public abstract void doPost(MyNioRequest request, MyNioResponse response);

    /**
     * io service方法，由顶层调用
     *
     * @param request
     * @param response
     */
    public void service(MyRequest request, MyResponse response) {
        // 判断请求方法
        String method = request.getMethod();
        if (Objects.equals(method, "GET")) {
            // 执行get请求
            doGet(request, response);
        } else if (Objects.equals(method, "POST")) {
            // 执行post请求
            doPost(request, response);
        }
    }

    public void service(MyNioRequest request, MyNioResponse response) {
        // 判断请求方法
        String method = request.getMethod();
        if (Objects.equals(method, "GET")) {
            // 执行get请求
            doGet(request, response);
        } else if (Objects.equals(method, "POST")) {
            // 执行post请求
            doPost(request, response);
        }
    }
    public void service(NettyRequest request, NettyResponse response) {
        // 判断请求方法
        String method = request.getMethod();
        if (Objects.equals(method, "GET")) {
            // 执行get请求
            doGet(request, response);
        } else if (Objects.equals(method, "POST")) {
            // 执行post请求
            doPost(request, response);
        }
    }
}
